package com.groupThree.wx.controller;

import com.groupThree.bean.Courier;
import com.groupThree.bean.Message;
import com.groupThree.bean.StateOfAU;
import com.groupThree.bean.User;
import com.groupThree.mvc.ResponseBody;
import com.groupThree.service.CourierService;
import com.groupThree.service.UserService;
import com.groupThree.util.JSONUtil;
import com.groupThree.util.RandomUtil;
import com.groupThree.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserController {

    @ResponseBody("/wx/loginSms.do")
    public String sendSms(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("phone");
        String code = RandomUtil.getCode() + "";
        //boolean flag = SMSUtil.loginSMS(userPhone,code);
        System.out.println(code);
        System.out.println(userPhone);
        boolean flag =true;
        Message message = new Message();
        if (flag){
            message.setStatus(0);
            message.setResult("短信发送成功！");
        }else {
            message.setStatus(-1);
            message.setResult("短信发送失败！");
        }
        UserUtil.setLoginSms(request.getSession(),userPhone,code);
        String json = JSONUtil.toJSON(message);
        return json;

    }

    @ResponseBody("/wx/login.do")
    public String login(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        String usercode = request.getParameter("code");
        String sysCode = UserUtil.getLoginSms(request.getSession(), userPhone);
        Message msg = new Message();

        if (sysCode == null){
            //手机获取短信
            msg.setStatus(-1);
            msg.setResult("手机号未获取到短信");

        }else if (sysCode.equals(usercode)){
            User user =new User();
            //手机号和短信验证码一致，登录成功
            //手机获取短信
            if (CourierService.findByC_Phone(userPhone) != null){
                //用户登录成功
                if (UserService.findByU_Phone(userPhone) == null){
                    user.setU_phone(userPhone);
                    user.setU_name(userPhone);
                    UserService.loginInsert(user);

                }
                //更改登录时间
                CourierService.updateLoginTime(userPhone);
                //快递员登录成功
                msg.setStatus(1);
                user.setUser(false);

            } else {
                //用户登录成功
                if (UserService.findByU_Phone(userPhone) == null){
                    user.setU_phone(userPhone);
                    user.setU_name(userPhone);
                    UserService.loginInsert(user);

                }
                //更改登录时间
                UserService.updateLoginTime(userPhone);
                msg.setStatus(0);
                user.setUser(true);

            }
            user.setU_phone(userPhone);
            UserUtil.setWxUser(request.getSession(),user);



        }else {
            //手机号和验证码不一致
            //手机获取短信
            msg.setStatus(-2);
            msg.setResult("验证码不一致，请检查是否输入正确");

        }
        String json = JSONUtil.toJSON(msg);
        return json;

    }

    @ResponseBody("/wx/adduser.do")
    public String addUser(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        String userName = request.getParameter("name");
        String password = request.getParameter("password");
        String idCard = null;
        String usercode = request.getParameter("code");
        String sysCode = UserUtil.getLoginSms(request.getSession(), userPhone);
        //查询id
        User user = UserService.findByU_Phone(userPhone);
        int id = user.getU_id();
        Message msg = new Message();
        User user2 = new User(userName, userPhone,idCard,password);
        System.out.println(user2);
        if (sysCode == null){
            //手机获取短信
            msg.setStatus(-1);
            msg.setResult("手机号未获取到短信");

        }else if (sysCode.equals(usercode)){

            StateOfAU update = UserService.update(id,user2);
            int flag = update.getStateAu();
            switch (flag){
                case 0:  //成功
                    msg.setStatus(0);
                    msg.setResult("录入成功！");
                    break;
                case 1:
                    msg.setStatus(1);
                    msg.setResult("电话号码已经存在！");
                    break;
                case 2:
                    msg.setStatus(2);
                    msg.setResult("身份证号码已经存在！");
                    break;
            }


        }else {
            //手机号和验证码不一致
            //手机获取短信
            msg.setStatus(-2);
            msg.setResult("验证码不一致，请检查是否输入正确");

        }
        String json = JSONUtil.toJSON(msg);
        return json;

    }

    /**
     * 获取用户信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/wx/userInfo.do")
    public String userInfo(HttpServletRequest request, HttpServletResponse response){
        User user = UserUtil.getWxUser(request.getSession());
        boolean isUser = user.isUser();
        Message message = new Message();
        if (isUser){
            message.setStatus(0);
        }else {
            message.setStatus(1);

        }
        message.setResult(user.getU_phone());
        String json = JSONUtil.toJSON(message);
        return json;
    }


    @ResponseBody("/wx/logout.do")
    public String logout(HttpServletRequest request, HttpServletResponse response){
       //销毁session
        request.getSession().invalidate();

        Message msg = new Message(0);
        return JSONUtil.toJSON(msg);
    }
}
